﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace YXShop.Web.admin.stocks
{
    public partial class out_stocks_list : System.Web.UI.Page
    {
        BLL.Stocks.OutProduct bllOutProduct = new BLL.Stocks.OutProduct();
        BLL.Stocks.BarCode bllBarCode = new BLL.Stocks.BarCode();
        BLL.Store.Store bllStore = new BLL.Store.Store();
        BLL.Store.StoreCenter bllStoreCenter = new BLL.Store.StoreCenter();
        BLL.Product.ProductInfo bllProductinfo = new BLL.Product.ProductInfo();
        BLL.Product.ProductType bllProductType = new BLL.Product.ProductType();
        BLL.Admin.Administrators bllAdministrator = new BLL.Admin.Administrators();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.hfStoreCenterid.Value.ToString() != "")
            {
                Model.Store.StoreCenter storecenterModel = bllStoreCenter.GetModelById(int.Parse(this.hfStoreCenterid.Value.ToString()));
                if (storecenterModel != null)
                {
                    this.txtStoreCenter.Text = storecenterModel.StoreCentername;
                }
            }
            if (this.hfStoreid.Value.ToString() != "")
            {
                Model.Store.Store storeModel = bllStore.GetModelById(int.Parse(this.hfStoreid.Value.ToString()));

                if (storeModel != null)
                {
                    this.txtStoreName.Text = storeModel.Storename;
                }
            }
            if (!IsPostBack)
            {
                string where = "";
                //YXShop.Common.PromptInfo.Popedom("014001001");
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if (admin == null)
                {
                    this.ltlMsg.Text = "用户操作失败，请重新登录";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                else
                {
                    where = getDataByAdmin(admin);
                }
                InitWebControl();
                this.lblList.Text = GetList(where);
            }
        }

        #region 绑定
        private string GetList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = bllOutProduct.GetList(where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("", "条码");
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "商品类别");
            table.AddHeadCol("", "仓库类型");
            table.AddHeadCol("", "仓库名称");
            table.AddHeadCol("", "待入库状态");
            table.AddHeadCol("", "备注");
            table.AddHeadCol("", "操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    Model.Stocks.OutProduct outProductModel = bllOutProduct.GetModelById(int.Parse(dataPage.DataReader["id"].ToString()));
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["id"].ToString() + "\" />");
                    Model.Stocks.BarCode barCodeModel = bllBarCode.GetModelById(int.Parse(dataPage.DataReader["barcodeid"].ToString()));
                    int productid = barCodeModel.Productid;
                    table.AddCol(barCodeModel.Barcode);
                    Model.Product.ProductInfo productModel = bllProductinfo.GetModel(productid);
                    table.AddCol(productModel.pro_Name);
                    //Model.Product.ProductType productTypeModel = bllProductType.GetModel(productModel.pro_Type);
                    table.AddCol(ProductClassName(productModel.pro_CID));
                    string stocktype = "";
                    if (outProductModel.StockTypeID == Model.Stocks.StockTypeEnum.CenterStock)
                    {
                        stocktype = "中央仓库";
                    }
                    if (outProductModel.StockTypeID == Model.Stocks.StockTypeEnum.AreaStock)
                    {
                        stocktype = "区域中心";
                    }
                    if (outProductModel.StockTypeID == Model.Stocks.StockTypeEnum.ShopStock)
                    {
                        stocktype = "门店仓库";
                    }
                    table.AddCol(stocktype);
                    string stockName = "";
                    if ((int)outProductModel.StockTypeID == 0)//中央仓库
                    {
                        stockName = "中央仓库";
                    }
                    if ((int)outProductModel.StockTypeID == 1)//区域中心
                    {

                        stockName = bllStoreCenter.GetModelById(outProductModel.StockID).StoreCentername;
                    }
                    if ((int)outProductModel.StockTypeID == 2)//门店
                    {
                        stockName = bllStore.GetModelById(outProductModel.StockID).Storename;
                    }
                    table.AddCol(stockName);
                    string state = "";
                    if((int)(outProductModel.Intype)==0)
                    {
                        state = "正常";
                    }
                    if ((int)(outProductModel.Intype) == 1)
                    {
                        state = "退货";
                    }
                    table.AddCol(state);
                    table.AddCol(dataPage.DataReader["notes"].ToString());
                    string strUrl = YXShop.TemplateAction.Common.UrlStr("In_Stocks_Edit", "id=" + dataPage.DataReader["id"].ToString(), "admin/stocks/");
                    table.AddCol(string.Format("<a href=\"" + strUrl + "\">入库  </a><a href='../../product/productcontent.aspx?q_productid={0}' target='_blank'>浏览</a>", productid));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            string str = "暂无归类";
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
                if (!string.IsNullOrEmpty(reStr))
                {
                    str = reStr;
                }
            }
            return str;
        }
        #endregion
        #region 控件初始化
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtStoreName.Attributes.Add("readonly", "readonly");
            this.txtStoreName.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfStoreid.ClientID + "," + this.txtStoreName.ClientID + "),310,450,'" + sp.DummyPaht + "');");

            this.txtStoreCenter.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid.ClientID + "," + this.txtStoreCenter.ClientID + "),310,450,'" + sp.DummyPaht + "');");
           
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion
        protected void linkQuery_Click(object sender, EventArgs e)
        {
            string where = "";
         
            if (DropDownListType.SelectedItem.Value != "2")
            {
                where += " and intype=" + DropDownListType.SelectedItem.Value;
            }
            if (DropDownList1.SelectedItem.Value != "4")
            {
                where += " and stocktypeid=" + DropDownList1.SelectedItem.Value;
                if (DropDownList1.SelectedItem.Value == "0")
                {
                    where += " and stockid = 0";
                }
                if (DropDownList1.SelectedItem.Value == "1")
                {
                    if (this.hfStoreCenterid.Value != "")
                    {
                        where += " and stockid = " + this.hfStoreCenterid.Value;
                    }
                }
                if (DropDownList1.SelectedItem.Value == "2")
                {
                    if (this.hfStoreid.Value != "")
                    {
                        where += " and stockid = " + this.hfStoreid.Value;
                    }
                }
            }
            this.lblList.Text = GetList(where);


        }
        #region  根据用户获取数据
        public string getDataByAdmin(Model.Admin.AdminInfo admin)
        {
            string result = "";
            if (admin.AdminType == 1)//中央仓库管理员
            {
                result = "";
            }
            if (admin.AdminType == 2)//区域仓库管理员
            {
                this.DropDownList1.Items.RemoveAt(0);
                List<Model.Store.StoreCenter> list = bllAdministrator.GetStoreCenterAdmin_StoreCenter(admin.AdminId);
                List<Model.Store.Store> listStore = bllAdministrator.GetStoreCenterAdmin_Store(admin.AdminId);
                string ids = "";//所管理的区域中心
                string ids2 = "";//所管理的门店
                if (list != null)
                {
                    if (list.Count > 0)
                    {
                        foreach (Model.Store.StoreCenter center in list)
                        {
                            if (ids == "")
                            {
                                ids = center.Sid.ToString();
                            }
                            else
                            {
                                ids += "," + center.Sid.ToString();
                            }
                        }
                    }
                }
                if (listStore != null)
                {
                    if (listStore.Count > 0)
                    {
                        foreach (Model.Store.Store store in listStore)
                        {
                            if (ids2 == "")
                            {
                                ids2 = store.Sid.ToString();
                            }
                            else
                            {
                                ids2 += "," + store.Sid.ToString();
                            }
                        }
                    }
                }
                if (ids != "")
                {
                    result = "and (stocktypeid=1 and stockid in(" + ids + ")) ";
                }
                if (ids2 != "")
                {
                    result += "and (stocktypeid=2 and stockid in(" + ids2 + ")) ";
                }

            }
            if (admin.AdminType == 3)//名店管理员
            {
                this.DropDownList1.Items.RemoveAt(0);
                this.DropDownList1.Items.RemoveAt(0);
                List<Model.Store.Store> listStore = bllAdministrator.GetStoreCenterAdmin_Store(admin.AdminId);
                string ids2 = "";//所管理的门店
                if (listStore != null)
                {
                    if (listStore.Count > 0)
                    {
                        foreach (Model.Store.Store store in listStore)
                        {
                            if (ids2 == "")
                            {
                                ids2 = store.Sid.ToString();
                            }
                            else
                            {
                                ids2 += "," + store.Sid.ToString();
                            }
                        }
                    }
                }
                if (ids2 != "")
                {
                    result = "and (stocktypeid=2 and stockid in()" + ids2 + ")) ";
                }
            }
            return result;
        }
        #endregion
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (DropDownList1.SelectedItem.Value == "0")//中央仓库
            {
                this.txtCenter.Visible = true;
                this.txtStoreCenter.Visible = false;
                this.txtStoreName.Visible = false;
                this.txtCenter.Text = "中央仓库";
            }
            if (DropDownList1.SelectedItem.Value == "1")//区域中心
            {
                this.txtCenter.Visible = false;
                this.txtStoreCenter.Visible = true;
                this.txtStoreName.Visible = false;
            }
            if (DropDownList1.SelectedItem.Value == "2")//门店
            {
                this.txtCenter.Visible = false;
                this.txtStoreCenter.Visible = false;
                this.txtStoreName.Visible = true;
            }
            if (DropDownList1.SelectedItem.Value == "4")//所有仓库
            {
                this.txtCenter.Visible = true;
                this.txtStoreCenter.Visible = false;
                this.txtStoreName.Visible = false;
                this.txtCenter.Text = "所有仓库";
            }
        }

    }
}